package com.iot.mqttbroker.commom;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.Serializable;
import java.util.HashMap;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY)
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 返回码
     */
    private String code;
    /**
     * 返回消息
     */
    private String msg;

    private String time;

    /**
     * 返回数据
     */
    private T data;

//1.成功处理-------------------------------------------------------------------------------------------------

    /**
     * 1.1全参数返回，不建议直接使用此方法（全参数）
     *
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static Result success(String code, String msg, Object data) {
        Result result = new Result();

        //赋值code,处理null和空
        if (null == code || "".equals(code)) {
            result.setCode("0");
        } else {
            result.setCode(code);
        }

        //赋值msg,处理null和空
        if (null == msg || "".equals(msg)) {
            result.setMsg("成功");
        } else {
            result.setMsg(msg);
        }

        result.setTime(String.valueOf(System.currentTimeMillis() / 1000));

        if (null == data) {
            result.setData(new HashMap<String, String>() {{
                put("success", "request success");
            }});
        } else {
            result.setData(data);
        }

        return result;
    }


    /**
     * 1.2仅传返回对象（1个参数）
     *
     * @param data
     * @return
     */
    public static Result success(Object data) {
        return success("0", "成功", data);
    }

    /**
     * 1.3仅返回成功状态（0参数）
     *
     * @return
     */
    public static Result success() {
        return success(null);
    }


    /**
     * 1.5仅传返回对象（1个参数）
     *
     * @param data
     * @return
     */
    public static <T> Result<T> successWithData(T data) {
        return success("0", "成功", data);
    }


//2.操作失败流程--------------------------------------------------------------------------------------------


    /**
     * 2.1操作失败流程(全参数)
     *
     * @param code
     * @param msg
     * @return
     */
    public static Result error(String code, String msg, Object data) {
        Result result = new Result();
        //赋值code,处理null和空
        if (null == code || "".equals(code)) {
            result.setCode("1");
        } else {
            result.setCode(code);
        }

        //赋值msg,处理null和空
        if (null == msg || "".equals(msg)) {
            result.setMsg("失败");
        } else {
            result.setMsg(msg);
        }

        result.setTime(String.valueOf(System.currentTimeMillis() / 1000));

        if (null == data) {
            result.setData(new HashMap<String, String>() {{
                put("error", "request error");
            }});
        } else {
            result.setData(data);
        }
        return result;
    }


    /**
     * 2.2重载，操作失败的方法（2参数 因为操作失败一般都不需要返回数据内容）
     *
     * @param code
     * @param msg
     * @return
     */
    public static Result error(String code, String msg) {
        return error(code, msg, null);
    }


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", time='" + time + '\'' +
                ", data=" + data +
                '}';
    }

    /**
     * 判断是否成功
     *
     * @param result
     * @return
     */
    public static boolean isSuccess(Result<?> result) {
        return ObjectUtil.isNotNull(result)
                && ObjectUtil.isNotNull(result.getCode())
                && ObjectUtil.equals(result.getCode(), "0");
    }

    /**
     * 判断是否成功且数据不为空
     *
     * @param result
     * @return
     */
    public static boolean successWithData(Result<?> result) {
        return isSuccess(result) && ObjectUtil.isNotNull(result.getData());
    }
}
